<?php
// 接口API**********************
//username 用户名
// 接口API**********************
//获取传入的参数
// $username = $_REQUEST['username'];
// $password = $_REQUEST['password'];
$name = $_REQUEST['name'];
$way = $_REQUEST['way'];
$peoplename = $_REQUEST['peoplename'];
$uid = $_REQUEST['uid'];
$useid = $_REQUEST['useid'];
$peopleid = $_REQUEST['peopleid'];
$goodsid = $_REQUEST['goodsid'];
$goods = $_REQUEST['goods'];
$goods = json_decode($goods);
//建立数据库连接
$link = mysqli_connect('localhost','root','123456','twoStage');
//判断连接成功与否
if(!$link){
    die('连接失败'.mysqli_connect_error());
};
//设置编码
mysqli_set_charset($link,'utf8');
if($way=='f5'){
//创建sql语句，找出目标账号
$search = "select * from account where username='$name'";
//执行sql语句
$searchDo = mysqli_query($link,$search);
$row = mysqli_fetch_assoc($searchDo);
$id = $row['id'];
//创建sql语句，找出目标账号
$search1 = "select * from session where user='$id'";
//执行sql语句
$search1Do = mysqli_query($link,$search1);
if ($search1Do) {
    $returnArr = [];
    while ($row = mysqli_fetch_assoc($search1Do)) {
        $iid=(int)$row['people'];
        $search2 = "select * from account where id='$iid'";
        //执行sql语句
        $search2Do = mysqli_query($link,$search2);
        $row2 = mysqli_fetch_assoc($search2Do);
        $row['name'] = $row2['username'];
        $returnArr[] = $row;
    }
    if (!empty($returnArr)) {
        //返回结果
        $data = [
            'status' => 1,
            'info' => $returnArr,
        ];
        echo json_encode($data);
    }
}
}else if($way=='talk'){
//创建sql语句，找出目标账号
$search = "select * from account where username='$peoplename'";
//执行sql语句
$searchDo = mysqli_query($link,$search);
$row = mysqli_fetch_assoc($searchDo);
$id = $row['id'];
//创建sql语句，找出目标账号
$search1 = "select * from account where username='$name'";
//执行sql语句
$search1Do = mysqli_query($link,$search1);
$row1 = mysqli_fetch_assoc($search1Do);
$id1 = $row1['id'];
echo json_encode([$id1,$id]);
}else if($way=='talk2'){
//创建sql语句，找出目标账号
$search = "select * from account where id='$uid'";
//执行sql语句
$searchDo = mysqli_query($link,$search);
$row = mysqli_fetch_assoc($searchDo);
$username = $row['username'];
echo $username;
}else if($way=='talk3'){
//创建sql语句，找出目标账号
$search = "select * from session where user='$useid' and people='$peopleid'";
//执行sql语句
$searchDo = mysqli_query($link,$search);
$row = mysqli_fetch_assoc($searchDo);
if($row){
}else{
$sql = "INSERT INTO session (user, people)VALUES ('$useid', '$peopleid')";
$sql1 = "INSERT INTO session (user, people)VALUES ('$peopleid', '$useid')";
mysqli_query($link,$sql);
mysqli_query($link,$sql1);
}
}else if($way=='addCollect'){
//创建sql语句，找出目标账号
$search1 = "select * from account where username='$name'";
//执行sql语句
$search1Do = mysqli_query($link,$search1);
$row1 = mysqli_fetch_assoc($search1Do);
$id1 = $row1['id'];
//创建sql语句，找出目标账号
$search = "select * from collect where user='$id1' and collect='$goodsid'";
//执行sql语句
$searchDo = mysqli_query($link,$search);
$row = mysqli_fetch_assoc($searchDo);
if($row){
   echo '您的收藏夹中已存在该商品';
}else{
    $sql = "INSERT INTO collect (user, collect) VALUES ('$id1', '$goodsid')";
    mysqli_query($link,$sql);
    echo '已成功加入收藏夹';
}
}else if($way=='seeCollect'){
//创建sql语句，找出目标账号
$search1 = "select * from account where username='$name'";
//执行sql语句
$search1Do = mysqli_query($link,$search1);
$row1 = mysqli_fetch_assoc($search1Do);
$id1 = $row1['id'];
//创建sql语句，找出目标账号
$search = "select * from collect where user='$id1'";
//执行sql语句
$searchDo = mysqli_query($link,$search);
$returnArr = [];
while($row = mysqli_fetch_assoc($searchDo)){
    $returnArr[] = $row;
}
if(empty($returnArr)){

}else{
    echo json_encode($returnArr);
}
}else if($way=='collectPage'){
$goods = join(',', $goods) ;
if($goods){
        //创建sql语句，找出目标账号
$search1 = "select * from goods where id in ({$goods})";
//执行sql语句
$search1Do = mysqli_query($link,$search1);
$returnArr = [];
while($row = mysqli_fetch_assoc($search1Do)){
    $returnArr[] = $row;
}
if(empty($returnArr)){

}else{
    echo json_encode($returnArr);
}
}
}else if($way=='usertype'){
    //创建sql语句，找出目标账号
$search1 = "select * from account where username='$name'";
//执行sql语句
$search1Do = mysqli_query($link,$search1);
$row1 = mysqli_fetch_assoc($search1Do);
$type1 = $row1['type'];
echo $type1;
}

//关闭数据库连接
mysqli_close($link);
?>